﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.IO;
using MEF_Test.Bricks;
using MEF_Test.Bricks.Actions;
using MEF_Test.Contracts;
using Microsoft.Practices.Unity;
using Synology.AudioStationApi;

namespace MEF_Test.Module.AudioStation
{
    public class AudioStationFileHandler
        : IFileHandler
    {


        // sort=title&dir=ASC&action=browse&target=musiclib_music_aa&server=musiclib_music_aa&category=&keyword={0}&start=0&limit=50
        //public string Sort { get; set; }
        //public ListSortDirection Dir { get; set; }
        public AudioStationAction Action { get; set; }
        
        public string Server { get; set; }
                        
        public AudioStationFileHandler(AudioStationAction audioStationAction, SynoItem item)
        {
            // TODO: Complete member initialization
            Action = audioStationAction;
            CanHaveChildren = item.IsContainer;            
            ItemID = item.ItemID;
            Sequence = item.Sequence;           
            ItemPid = item.ItemPid;
            IsTrack = item.IsTrack;
            AlbumArtUrl = item.AlbumArtUrl;
            Icon = item.Icon;            
            Support = item.Support;
            Title = item.Title;
        }
        

        public IMediaHandlerFactory MediaHandler { get; private set; }



        public string ContentType { get; private set; }

        public IEnumerable<INodeAction> GetSupportedActions(IBrowserNodeViewModel browserNodeBase)
        {
            var actionsManager = IoC.Current.Resolve<IActionsManager>();

            var v = new List<INodeAction>();

            foreach (var item in actionsManager.ExportedActions)
            {
                // Fixme : include the selectedProvider within the item.Value
                var compatibleProviders = item.Value.GetCompatibleProviders(browserNodeBase);
                var selectedProvider = browserNodeBase.SelectProvider(compatibleProviders, item.Value);
                v.Add(item.Value);
            }


            return v;

            
            // Gather all actions accepted by AudioStation Items and return them)
        }

        public bool CanHaveChildren { get; private set; }
        public bool IsTrack { get; set; }
        public string ItemPid{get; set ; }
        public string ItemID { get; set; }
        public int Sequence { get; set; }
        public string AlbumArtUrl {get; private set;}
        public string Icon { get; private set; }        
        public bool Support { get; private set; }
        public string Title { get; private set; }

        public Stream GetStream()
        {
            if (!IsTrack)
            {
                throw new InvalidOperationException("You can only get streams for tracks.");
            }
                        
            throw new NotImplementedException();
        }
    }

    public class AudioStationTrackFileHandler : AudioStationFileHandler
    {
        public AudioStationTrackFileHandler(AudioStationAction audioStationAction, SynoTrack item) : base(audioStationAction, item)
        {
            Album = item.Album;
            Artist = item.Artist;
            Bitrate = item.Bitrate;
            Channels = item.Channels;
            Class = item.Class;
            Disc = item.Disc;
            Duration = item.Duration;
            Genre = item.Genre;
            ProtocolInfo = item.ProtocolInfo;
            Res = item.Res;
            Sample = item.Sample;
            Size = item.Size;
            Track = item.Track;
            Year = item.Year;
            
        }

        protected string Album { get; set; }

        protected string Artist { get; set; }

        protected long Bitrate { get; set; }

        protected int Channels { get; set; }

        protected string Class { get; set; }

        protected int Disc { get; set; }

        protected TimeSpan Duration { get; set; }

        protected string Genre { get; set; }

        protected string ProtocolInfo { get; set; }

        protected string Res { get; set; }

        protected long Sample { get; set; }

        protected long Size { get; set; }

        protected int Track { get; set; }

        protected int Year { get; set; }

        public new Stream GetStream()
        {
            //throw new NotImplementedException();
            return new MemoryStream();
        }
    }
}